package com.bfly.mall.advert.controller;

import com.bfly.common.ResponseUtil;
import com.bfly.common.page.Pager;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.context.PagerThreadLocal;
import com.bfly.core.security.ActionModel;
import com.bfly.core.security.PermissionCheck;
import com.bfly.mall.advert.entity.AdvertType;
import com.bfly.mall.advert.dto.AdvertTypeFormDTO;
import com.bfly.mall.advert.dto.AdvertTypeListDTO;
import com.bfly.mall.advert.service.IAdvertTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 广告类型Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/12/22 22:37
 */
@RestController
@RequestMapping(value = "/manage/advert/type")
public class AdvertTypeController extends BaseController {

    @Autowired
    private IAdvertTypeService advertTypeService;

    /**
     * 广告类型列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/22 22:37
     */
    @GetMapping(value = "/list")
    public void list(HttpServletResponse response, HttpServletRequest request) {
        PagerThreadLocal.set(request);
        Pager<AdvertType> pager = advertTypeService.getPage(null);
        Pager<AdvertTypeListDTO> dtoPager = new Pager<>(pager.getPageNo(), pager.getPageSize(), pager.getTotalCount());

        if (pager.getData() != null) {
            List<AdvertTypeListDTO> list = new ArrayList<>();
            for (AdvertType type : pager.getData()) {
                AdvertTypeListDTO dto = new AdvertTypeListDTO();
                BeanUtils.copyProperties(type, dto);
                list.add(dto);
            }
            dtoPager.setData(list);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dtoPager));
    }

    /**
     * 所有广告类型
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/22 23:22
     */
    @GetMapping(value = "/all")
    @PermissionCheck(required = false)
    public void getAll(HttpServletResponse response, HttpServletRequest request) {
        List<AdvertType> advertTypes = advertTypeService.getList();
        List<AdvertTypeListDTO> list = new ArrayList<>();

        if (advertTypes != null) {
            for (AdvertType type : advertTypes) {
                AdvertTypeListDTO dto = new AdvertTypeListDTO();
                BeanUtils.copyProperties(type, dto);
                list.add(dto);
            }
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(list));
    }

    /**
     * 新增广告类型
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/22 22:40
     */
    @PostMapping(value = "/add")
    @ActionModel(value = "新增广告类型")
    public void saveAdvertType(HttpServletResponse response, @Valid @RequestBody AdvertTypeFormDTO formDTO, BindingResult result) {
        validData(result);
        advertTypeService.save(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 编辑广告类型
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/12/22 22:41
     */
    @PostMapping(value = "/edit")
    @ActionModel(value = "编辑广告类型")
    public void editAdvertType(HttpServletResponse response, @Valid @RequestBody AdvertTypeFormDTO formDTO, BindingResult result) {
        validData(result);
        advertTypeService.edit(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 删除广告类型
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/20 12:26
     */
    @PostMapping(value = "/delete")
    @ActionModel(value = "删除广告类型")
    public void removeAdvertType(HttpServletResponse response, @RequestBody Integer... adIds) {
        advertTypeService.remove(adIds);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}